package com.sphd.restaurantsystem.adapters;


import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.info.CouponInfo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by g on 2015/6/26.
 * 选择优惠券的适配器
 */
public class SelectCouponsAdapter extends BaseAdapter {
    private List<CouponInfo> infos;

    private double money = 0;//消费金额
    private double quanmoney = 0;//消费金额

    String gmtTime;

    public SelectCouponsAdapter(List<CouponInfo> infos, double allMoney, double quanmoney,
                                String gmtTime) {
        this.infos = infos;

        this.money = allMoney;

        this.quanmoney = quanmoney;
        this.gmtTime = gmtTime;

    }

    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.selectcoupons_item, null);
            viewHolder = new ViewHolder();
            viewHolder.coupinsmsg = (TextView) convertView.findViewById(R.id.tv_coupinsmsg);
            viewHolder.use = (CheckBox) convertView.findViewById(R.id.tv_use);
            viewHolder.pastdue = (TextView) convertView.findViewById(R.id.tv_pastdue);
            viewHolder.cutmoney = (TextView) convertView.findViewById(R.id.cut_money);
            viewHolder.selectcoupons = (LinearLayout) convertView.findViewById(R.id.ll_selectcoupons);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CouponInfo couponInfo = infos.get(position);
        viewHolder.coupinsmsg.setText("消费满" + couponInfo.getCard_upperlimit() + "元可以使用");
        viewHolder.cutmoney.setText(couponInfo.getCard_value() + "元");

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(gmtTime));
        cal.add(Calendar.DATE, couponInfo.getCard_days());

        Calendar fa = Calendar.getInstance();
        fa.setTime(new Date(couponInfo.getRegtime()));
        fa.add(Calendar.DATE, 1);
        viewHolder.pastdue.setText(new SimpleDateFormat("yyyy.MM.dd").format(fa.getTime())
                + "~" + (new SimpleDateFormat("yyyy.MM.dd")).format(cal.getTime()));
        viewHolder.use.setChecked(couponInfo.isChecked());
        viewHolder.use.setOnClickListener(view -> {
            // TODO: 2016/10/12  先判断 checkbox 是否为选中状态
            //如果现在为选中状态，点击则为取消状态 优惠券使用额减去对应的金额 设置bean中checkbox的状态（可直接设置为false 也可通过控件得到状态）
            if (!viewHolder.use.isChecked()) {
                Log.e("ALL1", money + "////" + quanmoney + "////");
                quanmoney = quanmoney - couponInfo.getCard_upperlimit();
                couponInfo.setChecked(viewHolder.use.isChecked());
            } else {
                if (quanmoney + couponInfo.getCard_upperlimit() <= money) {
                    Log.e("ALL2", money + "////" + quanmoney + "////");
                    quanmoney = quanmoney + couponInfo.getCard_upperlimit();
                    couponInfo.setChecked(viewHolder.use.isChecked());
                } else {
                    Log.e("ALL3", money + "////" + quanmoney + "////");
                    viewHolder.use.setChecked(false);
                    Snackbar.make(view, "很抱歉，您未达到使用标准。", Snackbar.LENGTH_SHORT).show();
                    couponInfo.setChecked(false);
                }
            }


        });


        return convertView;
    }

    private static class ViewHolder {
        private TextView coupinsmsg;
        private CheckBox use;
        private TextView pastdue;
        private TextView cutmoney;
        private LinearLayout selectcoupons;
    }

}
